package com.jiang.jixiang.ui.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.jiang.jixiang.R;
import com.jiang.jixiang.api.ApiUrl;
import com.jiang.jixiang.bean.OrderListBean;
import com.jiang.jixiang.databinding.FragmentInviteBinding;
import com.jiang.jixiang.databinding.FragmentOrderBinding;
import com.jiang.jixiang.network.CommonalityModel;
import com.jiang.jixiang.network.NetWorkListener;
import com.jiang.jixiang.network.okHttpModel;
import com.jiang.jixiang.ui.MyOrderInfoActivity;
import com.jiang.jixiang.ui.adapter.OrderAdapter;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;
import com.ws.universal.tools.base.BaseFragment;
import com.ws.universal.tools.utils.FileUtils;
import com.ws.universal.tools.utils.GsonUtils;
import com.ws.universal.tools.utils.ToastUtils;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

/**
 * 订单列表
 */
public class OrderFragment extends BaseFragment implements OnRefreshListener, OnLoadMoreListener, NetWorkListener {
    private FragmentOrderBinding binding;
    private OrderAdapter orderAdapter;
    private OrderListBean orderListBean;
    private int pageNumber = 1;
    private int pages;

    @Override
    public View getRootView(LayoutInflater inflater, ViewGroup container) {
        binding = FragmentOrderBinding.inflate(inflater, container, false);
        return binding.getRoot();
    }

    @Override
    public void initView() {
        binding.refreshLayoutlive.setOnRefreshListener(this);//加载更多
        binding.refreshLayoutlive.setOnLoadMoreListener(this);//加载更多
        binding.refreshLayoutlive.autoRefresh();//自动刷新
        LinearLayoutManager manager = new LinearLayoutManager(getContext());
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        binding.rcyData.setLayoutManager(manager);
        orderAdapter = new OrderAdapter();
        binding.rcyData.setAdapter(orderAdapter);

        orderAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                Bundle   bundle = new Bundle();
                bundle.putInt("status", orderAdapter.getData().get(position).getStatus());
                bundle.putString("order_no", orderAdapter.getData().get(position).getOrder_no());

                pageJump(MyOrderInfoActivity.class, bundle);
            }
        });


    }

    @Override
    public void autoData() {
        loadData();
    }

    private void loadData() {
        Map<String, Object> params = new HashMap<>();
        params.put("page_no", pageNumber);
        params.put("page_size", "10");
        okHttpModel.postJson(ApiUrl.orderList_Api, params, ApiUrl.orderList_Api_ID, this);
    }

    @Override
    public void onSucceed(JSONObject jsonData, int id, CommonalityModel commonality) {
        stopProgressDialog();
        JSONObject data = jsonData.optJSONObject("data");
        switch (id) {
            case ApiUrl.orderList_Api_ID:
                orderListBean = GsonUtils.parseJObject(data.toString(), OrderListBean.class);
                if (orderListBean != null) {
                    pages = orderListBean.getTotal();
                    if (pageNumber > 1) {
                        for (int i = 0; i < orderListBean.getList().size(); i++) {
                            orderAdapter.getData().add(orderListBean.getList().get(i));
                        }
                        orderAdapter.notifyDataSetChanged();
                    } else {
                        orderAdapter.setList(orderListBean.getList());
                    }
                    if (orderAdapter.getData().size() == 0) {
                        binding.viewStub.inflate();
                    }
                }
                break;
        }


    }

    @Override
    public void onFail(JSONObject jsonData, int id, CommonalityModel commonality) {
        stopProgressDialog();
        ToastUtils.showShort(commonality.getMessage());
    }

    @Override
    public void onError(int id, Exception e) {
        stopProgressDialog();
        ToastUtils.showShort(e.getMessage());
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        if (refreshLayout != null) {
            pageNumber = 1;
            loadData();
            refreshLayout.finishRefresh();
            refreshLayout.resetNoMoreData();//setNoMoreData(false);//重置初始状态
        }
    }

    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
        if (refreshLayout != null) {
            if (pageNumber >= pages) {
                refreshLayout.finishLoadMoreWithNoMoreData();//将不会再次触发加载更多事件
            } else {
                pageNumber++;
                loadData();
                refreshLayout.finishLoadMore();
            }
        }
    }

}
